package qc.powerjob.worker.processors.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import qc.module.demo.dto.water.WaterGetYnswjDataConditionDto;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时获取云南水文局数据接口保存数据
 */
@Component
public class GetYnswjDataProcessor implements BasicProcessor {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Override
    public ProcessResult process(TaskContext taskContext) throws Exception {
        System.out.println("定时调用获取云南水文局数据接口开始");

        ServiceInstance service = discoveryClient.getInstances("module-demo").get(0);

        //请求路径：分别为水库数据请求和河道数据请求的路径
        String reservoirUrl = "http://" + service.getHost() + ":" + service.getPort() + "/water/ynswj";
        String riverUrl = "http://" + service.getHost() + ":" + service.getPort() + "/water/ynswj";
        System.out.println(reservoirUrl);
        System.out.println(riverUrl);

        //设置编码
        restTemplate.getMessageConverters().set(0x1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

        //请求头
        HttpHeaders headers = new HttpHeaders();

        //设置请求参数，请求的接口参数为RequestBody对象
        //请求接口查询条件：开始时间，结束时间，每天0点到8点
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = new Date();
        start.setHours(0);
        start.setMinutes(0);
        start.setSeconds(0);
        Date end = new Date();
        end.setHours(8);
        end.setMinutes(0);
        end.setSeconds(0);
        String startTm = format.format(start);
        String endTm = format.format(end);
        System.out.println("startTm：" + startTm + ",endTm：" + endTm);

        //水库请求conditionDto
        WaterGetYnswjDataConditionDto reservoirCondition = new WaterGetYnswjDataConditionDto();
        reservoirCondition.setBegintm(startTm);
        reservoirCondition.setEndtm(endTm);
        reservoirCondition.setType("1");
        HttpEntity<WaterGetYnswjDataConditionDto> reservoirEntity = new HttpEntity<WaterGetYnswjDataConditionDto>(reservoirCondition, headers);
        System.out.println(reservoirEntity + "==========");

        //河道请求conditionDto
        WaterGetYnswjDataConditionDto riverCondition = new WaterGetYnswjDataConditionDto();
        riverCondition.setBegintm(startTm);
        riverCondition.setEndtm(endTm);
        riverCondition.setType("2");
        HttpEntity<WaterGetYnswjDataConditionDto> riverEntity = new HttpEntity<WaterGetYnswjDataConditionDto>(riverCondition, headers);
        System.out.println(riverEntity + "==========");

        //响应类型
        ParameterizedTypeReference<String> responseType = new ParameterizedTypeReference<String>() {
        };

        //请求水库数据
        ResponseEntity<String> reservoirResponse = restTemplate.exchange(
                reservoirUrl,//获取资源的地址
                HttpMethod.POST, //请求类型
                reservoirEntity, //请求实体
                responseType //响应类型
        );
        String result = reservoirResponse.getBody();
        System.out.println(result + "-=-=-=-");

        //请求河道数据
        ResponseEntity<String> riverResponse = restTemplate.exchange(
                riverUrl,//获取资源的地址
                HttpMethod.POST, //请求类型
                riverEntity, //请求实体
                responseType //响应类型
        );
        String riverResult = riverResponse.getBody();
        System.out.println(riverResult + "-=-=-=-");

        return new ProcessResult(true, "云南水文局水库、河道数据获取完成！！！");
    }
}
